package com.glory.brepda.activity.mfg;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.MaterielAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.EqpEntity;
import com.glory.brepda.entity.LotAndPackagingfileEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.MultiEqpBean;
import com.glory.brepda.entity.WoPartWobomline;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.entity.response.ReturnBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class StripPrintActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etWoID)
    FreeUI_ClearEditText etWoID;
    @BindView(R.id.etPrintNum)
    FreeUI_ClearEditText etPrintNum;
    @BindView(R.id.tvRLF)
    TextView tvRLF;
    @BindView(R.id.tvIsPrintQty)
    TextView tvIsPrintQty;
    @BindView(R.id.tvStripQty)
    TextView tvStripQty;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_strip_print;
    }

    @Override
    protected void initView() {

        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvBaseSubmit.setText("下一步");
        tvTitle.setText(title);
        setMainLayout(llMain);
    }

    @Override
    protected void initData() {

        initEdit(etWoID,s ->{

            //通过片号获取相关信息
            FreeUI_EntityCallBack<BaseResponseBean<WoPartWobomline>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<WoPartWobomline>>
                    (new TypeToken<BaseResponseBean<WoPartWobomline>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<WoPartWobomline>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        //赋值框架型号
                        for(int i=0;i<response.body().getResponse().getBody().getWORKORDERBOMLINELIST().size();i++){
                            if(response.body().getResponse().getBody().getWORKORDERBOMLINELIST().get(i).getMaterialType().equals("RLF")){
                                tvRLF.setText(response.body().getResponse().getBody().getWORKORDERBOMLINELIST().get(i).getMATERIALDESC());
                                break;
                            }
                        }
                        tvIsPrintQty.setText(response.body().getResponse().getBody().getISPRINTSTRIPQTY());
                        tvStripQty.setText(response.body().getResponse().getBody().getSTRIPQTY());
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<WoPartWobomline>> response) {
                    super.onError(response);
                    loadError(response.getException(), "失败");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("WORDER", getText(etWoID));
            map.addProperty("ACTION", "WORDER_QUERY");
            NetUtils.requestPostNet("IF.WORDER_QUERY_INFOS", "Common", getApplication(), map, callBack);
        });
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if(getText(etPrintNum).length()!=0 && Integer.parseInt(getText(etPrintNum))>Integer.parseInt(tvStripQty.getText().toString())){
                    showToast("打印数量不能大于任务号总条数",true);
                    return;
                }
                if (getText(etWoID).equals("")){
                    showToast("请扫描任务号",true);
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString("tvRLF",tvRLF.getText().toString());
                bundle.putString("PRINTCOUNT",getText(etPrintNum).length()==0?tvStripQty.getText().toString():getText(etPrintNum));
                bundle.putString("WOID",getText(etWoID));
                Intent intent = new Intent(this, StripPrint2Activity.class);
                intent.putExtras(bundle);
                startActivityForResult(intent,1);

        }
    }
}
